package com.jrx.anytxn.transaction.job.waiveAutoPayment.config;

import com.jrx.anytxn.param.entity.PrOrganizationTable;
import com.jrx.anytxn.param.service.system.IOrganizationTableService;
import com.jrx.anytxn.transaction.config.AnytxnBatchProperties;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import com.jrx.anytxn.transaction.entity.AmWaiveRepaymentPlanInfo;
import com.jrx.anytxn.transaction.job.waiveAutoPayment.bean.WaivePaymentLogBean;
import com.jrx.anytxn.transaction.job.waiveAutoPayment.step.WaiveAutoPaymentPreProcessor;
import com.jrx.anytxn.transaction.job.waiveAutoPayment.step.WaiveAutoPaymentPreReader;
import com.jrx.anytxn.transaction.job.waiveAutoPayment.step.WaiveAutoPaymentPreWriter;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.ItemReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.transaction.PlatformTransactionManager;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 贴息还款批扣准备
 *
 * @author : zhenghaopu
 * 2019/11/22
 */
@Configuration
@EnableConfigurationProperties(AnytxnBatchProperties.class)
public class WaiveAutoPaymentPreConfiguration {

    @Resource(name = "bizSqlSessionFactory")
    SqlSessionFactory sqlSessionFactory;

    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Autowired
    public StepBuilderFactory stepBuilderFactory;


    @Autowired
    private AnytxnBatchProperties anytxnBatchProperties;

    @Autowired
    private WaiveAutoPaymentPreProcessor waiveAutoPaymentPreProcessor;

    @Autowired
    private WaiveAutoPaymentPreWriter waiveAutoPaymentPreWriter;

    @Autowired
    private IOrganizationTableService organizationTableService;

    @Autowired
    private PlatformTransactionManager platformTransactionManager;

    @Bean
    public Job waiveAutoPaymentPreJob(){
        return jobBuilderFactory.get("waiveAutoPaymentPreJob")
                .start(waiveAutoPaymentPre())
                .build();
    }

    @Bean
    public Step waiveAutoPaymentPre(){
        return stepBuilderFactory.get("waiveAutoPaymentPre")
                .<AmWaiveRepaymentPlanInfo, WaivePaymentLogBean>chunk(anytxnBatchProperties.getAutoPaymentConfiguration().getChunkLimit())
                //表示一批处理100
                // 条记录，数据是逐条发送给processor处理的，处理100条记录后，统一返回给writer进行保存处理
                .reader(waiveAutoPaymentPreReader())
                .processor(waiveAutoPaymentPreProcessor)
                .writer(waiveAutoPaymentPreWriter)
                .transactionManager(platformTransactionManager)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anytxnBatchProperties.getAutoPaymentConfiguration().getThrottleLimit())//多线程*/
                .build();
    }


    @Bean
    @StepScope
    public ItemReader<AmWaiveRepaymentPlanInfo> waiveAutoPaymentPreReader() {
        PrOrganizationTable org = organizationTableService.findByOrgIdForBatch(TransBizConstant.DEFAULT_ORG_ID);
        Date businessDate = org.getNextProcessingDate();
        return new WaiveAutoPaymentPreReader(businessDate,sqlSessionFactory, anytxnBatchProperties.getAutoPaymentConfiguration().getPageSize());
    }

}
